package ide;

import java.io.File;

import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import datastruct.Column;
import datastruct.DBStructure;
import datastruct.Index;
import datastruct.Relation;
import datastruct.Relations;
import datastruct.Table;
import datastruct.Tables;

import xml.XMLReader;

public class TreeView extends JTree implements TreeSelectionListener{
	private static final long serialVersionUID = 1L;
	private static DefaultMutableTreeNode top = new DefaultMutableTreeNode("DATABASE");
	private static File file;
	
	public TreeView()
	{
		super(top);
		this.setShowsRootHandles(true);
		this.setRootVisible(true);
	}
	
	public void read(File fileXML)
	{
		file=fileXML;
		createNode();
	}
	
	private void createNode()
	{
		System.out.println("CREAZIONE STRUTTURA");
		
		DefaultMutableTreeNode nodeTables = new DefaultMutableTreeNode("TABELLE");
		DefaultMutableTreeNode nodeTable = new DefaultMutableTreeNode("TABLES");
		DefaultMutableTreeNode nodeColumns = new DefaultMutableTreeNode("TABELLE");
		DefaultMutableTreeNode nodeColumn = new DefaultMutableTreeNode("COLUMNS");
		DefaultMutableTreeNode nodeIndexes = new DefaultMutableTreeNode("TABELLE");
		DefaultMutableTreeNode nodeIndex = new DefaultMutableTreeNode("INDEXES");
		DefaultMutableTreeNode nodeRelations= new DefaultMutableTreeNode("RELAZIONI");
		DefaultMutableTreeNode nodeRelation = new DefaultMutableTreeNode("RELATIONS");
		
		top.removeAllChildren();
		
		XMLReader xml = new XMLReader(file);
		DBStructure dbStruct = xml.read();
		Tables tables =  dbStruct.getTables();			System.out.println("TABELLE  :" + tables.size());
		Relations relations = dbStruct.getRelations(); 	System.out.println("RELAZIONI:" + relations.size());
		// LEGGE IL FILE XML E CREA LA STRUTTURA PER IL JTREE
		for (int i=0;i<tables.size();i++)
		{
			top.add(nodeTables);
			
			Table t = tables.get(i);
			nodeTable = new DefaultMutableTreeNode(t.getName());
			nodeColumns = new DefaultMutableTreeNode("COLONNE");
			nodeIndexes = new DefaultMutableTreeNode("INDICI");
			
			nodeTables.add(nodeTable);
			nodeTable.add(nodeColumns);
			nodeTable.add(nodeIndexes);
			
			for (int j=0;j<t.getColumns().size();j++)
			{
				Column c = t.getColumns().get(j);
				nodeColumn = new DefaultMutableTreeNode(c.getName());
				nodeColumns.add(nodeColumn);
			}
			
			for (int j=0;j<t.getIndexes().size();j++)
			{
				Index idx = t.getIndexes().get(j);
				if(idx.isPrimaryKey())
					nodeIndex = new DefaultMutableTreeNode(idx.getName() + "*" );
				else
					nodeIndex = new DefaultMutableTreeNode(idx.getName());
				nodeIndexes.add(nodeIndex);
			}
		}
		
		nodeRelations = new DefaultMutableTreeNode("RELAZIONI");
		top.add(nodeRelations);
		
		for (int j=0;j<relations.size();j++)
		{
			Relation rel = relations.get(j);
			DefaultMutableTreeNode relName    = new DefaultMutableTreeNode(rel.getName());
			DefaultMutableTreeNode relPkTable = new DefaultMutableTreeNode(rel.getPkIndex().getTableName() + " (PK)");
			DefaultMutableTreeNode relPkIndex = new DefaultMutableTreeNode(rel.getPkIndex().getName());
			DefaultMutableTreeNode relFkTable = new DefaultMutableTreeNode(rel.getFkIndex().getTableName() + " (FK)");
			DefaultMutableTreeNode relFkIndex = new DefaultMutableTreeNode(rel.getFkIndex().getName());
			
			relPkTable.add(relPkIndex);
			relName.add(relPkTable);
			relFkTable.add(relFkIndex);
			relName.add(relFkTable);
			nodeRelations.add(relName);
		}
		
		System.out.println("STRUTTURA CREATA");
		
		DefaultTreeModel model = (DefaultTreeModel) this.getModel();
		model.reload();
		
		this.repaint();
	}
	
	public static void setFileXML(File fileXML)
	{
		file = fileXML;
	}
	
	@Override
	public void valueChanged(TreeSelectionEvent e) {
		// TODO Auto-generated method stub
		
	}

}
